<?php

namespace app\api\Logic\Campusclass;

use app\api\Logic\ManystoreShopSettingLogic;
use app\api\model\CampusClassCourseDateModel;
use app\api\model\CampusClassLeaveModel;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;

//请假接口
class CampusClassLeaveLogic
{
    /**
     * 学员是否允许请假
     * @param $shopId
     * @param $userId
     * @return bool
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @throws Exception
     */
    public function isCan($shopId,$userId){
        $manyStoreRule = new ManystoreShopSettingLogic();
        //获取校区规则
        $ruleInfo = $manyStoreRule->ruleInfo($shopId);
        if(empty($ruleInfo)){
            return true;
        }

        $startTime = strtotime(date('Y-m-01'));
        $endTime = strtotime(date('Y-m-d',strtotime('+1 month'))) - 1;

        //当允许请假次数大于0时才需判断，否则不允许请假
        if($ruleInfo['leave_num'] > 0){
            if($ruleInfo['leave_type'] == 1){
                //不限时间
                $count = CampusClassLeaveModel::where('user_id',$userId)->count();
            }else{
                //每月
                $count = CampusClassLeaveModel::where('user_id',$userId)
                    ->whereBetween('createtime',[$startTime,$endTime])
                    ->count();
            }

            if($count >= $ruleInfo['leave_num']){
                return false;
            }
            return true;
        }else{
            return false;
        }
    }

    /**
     * 判断是否重复请假
     * @param $shopId
     * @param $userId
     * @param $startTime
     * @param $endTime
     * @return bool
     * @throws Exception
     */
    public function isRepeat($shopId,$userId,$startTime,$endTime){
        $count = CampusClassLeaveModel::where('shop_id',$shopId)
            ->where('user_id',$userId)
            ->where(function ($query) use ($startTime,$endTime){
                $query->where(function ($sQuery) use ($startTime, $endTime) {
                    $sQuery->where('start_time', '<=', $startTime)->where('end_time', '>=', $endTime);
                })->whereOr(function ($sQuery) use ($startTime, $endTime) {
                    $sQuery->where('start_time', '>=', $startTime)->where('end_time', '<=', $endTime);
                })->whereOr(function ($sQuery) use ($startTime, $endTime) {
                    $sQuery->where('start_time', '>=', $startTime)->where('start_time', '<=', $endTime);
                })->whereOr(function ($sQuery) use ($startTime, $endTime) {
                    $sQuery->where('end_time', '>=', $startTime)->where('end_time', '<=', $endTime);
                });
            })->count();
        return $count > 0;
    }

    /**
     * 获取请假详情
     * @param $leaveId
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getInfo($leaveId){
        $info = CampusClassLeaveModel::where('id',$leaveId)
            ->find();
        return objToArray($info) ?? [];
    }
}
